package chuangyi.com.org.DOMIHome.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.databinding.ObservableArrayList;
import android.databinding.ObservableList;
import android.databinding.ViewDataBinding;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zxh on 16/11/16.
 */
public class ListBindingAdapter<T extends BindingListItem> extends ArrayAdapter<T> {
    public static final int ACTION_ADD = 1;
    public static final int ACTION_DEL = 2;
    public static final int ACTION_REFRESH = 3;

    private List<T> added;


    private Context mContext;
    private int variableId;
    private HashMap<Integer, Integer> typeMap;

    public Context getContext() {
        return mContext;
    }

    public void setVariableId(int variableId) {
        this.variableId = variableId;
    }

    public void setViewType(List<Integer> viewTypeList) {
        int typeId = 0;
        for (Integer viewType : viewTypeList) {
            typeMap.put(viewType, typeId);
            typeId++;
        }
        notifyDataSetInvalidated();
    }

    public ListBindingAdapter(Context context, int variableId) {
        this(context, variableId, null);
    }

    /**
     * 新建绑定列表适配器
     *
     * @param context
     * @param variableId
     */
    public ListBindingAdapter(Context context, int variableId, List<Integer> viewTypeList) {
        super(context, 0, new ArrayList<T>());
        this.mContext = context;
        this.variableId = variableId;
        this.typeMap = new HashMap<Integer, Integer>();
        if (viewTypeList != null) {
            setViewType(viewTypeList);
        }
        added = new ArrayList<T>();
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewDataBinding dataBinding = null;
        T item = getItem(i);
        if (view == null) {
            dataBinding = DataBindingUtil.inflate(LayoutInflater.from(viewGroup.getContext()), item.getLayout(), viewGroup, false);
        } else {
            dataBinding = DataBindingUtil.findBinding(view);
            if (dataBinding == null) {
                dataBinding = DataBindingUtil.inflate(LayoutInflater.from(viewGroup.getContext()), item.getLayout(), viewGroup, false);
            }
        }
        dataBinding.setVariable(variableId, item);
        view = dataBinding.getRoot();
        return view;
    }

    @Override
    public int getViewTypeCount() {
        return typeMap.size() == 0 ? 1 : typeMap.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (typeMap.size() == 0) {
            return 0;
        }
        T item = super.getItem(position);
        return typeMap.get(item.getLayout());
    }

    @Override
    public void add(T item) {
        synchronized (added) {
            added.add(item);
        }
    }

    @Override
    public void notifyDataSetChanged() {
        synchronized (added) {
            for (T item : added) {
                super.setNotifyOnChange(false);
                super.add(item);
            }
            added.clear();
        }
        super.notifyDataSetChanged();
    }

    public void clear(boolean notify) {
        synchronized (added) {
            super.setNotifyOnChange(notify);
            clear();
        }
    }
}
